package cn.heapstack.jpa.entityrelationships.domain;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity(name="EntityRelationships_Customer")
public class Customer {


	private long id;
	private String firstName;
	private String lastName;
	private Address address;
	private CreditCard creditCard;
	private Collection<Phone> phoneNumbers;
	private Collection<Reservation> reservations = new ArrayList<Reservation>();
	
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="ADDRESS_ID")
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	@OneToOne(targetEntity=CreditCard.class,cascade=CascadeType.ALL)
	@JoinColumn(name="CREDIT_CARD_ID")
	public CreditCard getCreditCard() {
		return creditCard;
	}
	public void setCreditCard(CreditCard creditCard) {
		this.creditCard = creditCard;
	}
	
	/**
	 * join table mapping
	 * 使用关联表映射 的方式进行单向的一对多关联
	 * 
	 * 也可是直接使用@JoinColumn(name="CUSTOMER_ID")实现OneToMany的关系映射
	 * @return
	 */
	@OneToMany(cascade=CascadeType.ALL)
	@JoinTable(name="EntityRelationships_OneToMany_CUSTOMER_PHONE",
			joinColumns={@JoinColumn(name="CUSTOMER_ID")},
			inverseJoinColumns={@JoinColumn(name="PHONE_ID")})
	public Collection<Phone> getPhoneNumbers() {
		return phoneNumbers;
	}
	public void setPhoneNumbers(Collection<Phone> phoneNumbers) {
		this.phoneNumbers = phoneNumbers;
	}
	
	@ManyToMany(mappedBy="customers")
	public Collection<Reservation> getReservations() {
		return reservations;
	}
	public void setReservations(Collection<Reservation> reservations) {
		this.reservations = reservations;
	}

	
	
}

